package com.chengzhi.disruptor.heigh;

import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        Disruptor<Trade> disruptor = new Disruptor<>(() -> new Trade(), 1024 * 1024, r -> {
            return new Thread(r);
        }, ProducerType.SINGLE, new BusySpinWaitStrategy());
        //设置多消费者

        //串行化
        /*disruptor
                .handleEventsWith(new TradeHandler1())
                .handleEventsWith(new TradeHandler2())
                .handleEventsWith(new TradeHandler3())
                .handleEventsWith(new TradeHandler4())
                .handleEventsWith(new TradeHandler5())
                .handleEventsWith(new TradeHandler3());*/
        //并行化方式一
//        disruptor.handleEventsWith(new TradeHandler1(),new TradeHandler2(), new TradeHandler3());

        //并行化方式二
        /*disruptor.handleEventsWith(new TradeHandler1());
        disruptor.handleEventsWith(new TradeHandler2());
        disruptor.handleEventsWith(new TradeHandler3());*/

        //菱形操作   handler2
        //          /       \
        //     handler1    hangler4
        //          \       /
        //           hangler3
        //方式一
        /*disruptor.handleEventsWith(new TradeHandler1()).handleEventsWith(new TradeHandler2(), new TradeHandler3())
                .handleEventsWith(new TradeHandler4());*/
        //方式二
        /*EventHandlerGroup<Trade> handlerGroup = disruptor.handleEventsWith(new TradeHandler1())
                .handleEventsWith(new TradeHandler2(), new TradeHandler3());
        handlerGroup.then(new TradeHandler4());*/


        //多边形操作
        //      handler1 --- handler2
        //     /                     \
        //  start                     handler3
        //     \                     /
        //      handler4 --- handler5
        TradeHandler1 handler1 = new TradeHandler1();
        TradeHandler2 handler2 = new TradeHandler2();
        TradeHandler3 handler3 = new TradeHandler3();
        TradeHandler4 handler4 = new TradeHandler4();
        TradeHandler5 handler5 = new TradeHandler5();


        disruptor.handleEventsWith(handler1, handler4);
        disruptor.after(handler1).handleEventsWith(handler2);
        disruptor.after(handler4).handleEventsWith(handler5);
        disruptor.after(handler2, handler5)
                .handleEventsWith(handler3);

        disruptor.start();
        CountDownLatch latch = new CountDownLatch(1);
        executorService.submit(new TradePublisher(disruptor, latch));
        latch.await();
        disruptor.shutdown();
        executorService.shutdown();
    }
}
